package com.staff.servlet.user;

import com.staff.model.*;
import com.staff.model.common.*;

import com.staff.service.*;

import com.staff.utils.*;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.io.IOException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class UserStaffServlet extends HttpServlet {
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
    SimpleDateFormat sdf3 = new SimpleDateFormat("yyyyMMddHHmmss");

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
        String method = request.getParameter("method");
        response.setCharacterEncoding("UTF-8");

        if ("goIndex".equals(method)) {
            goIndex(request, response);
        }

        if ("getDataList".equals(method)) {
            getDataList(request, response);
        }
    }

    private void goIndex(HttpServletRequest request,
        HttpServletResponse response) throws IOException {
        LoginModel login = (LoginModel) request.getSession()
                                               .getAttribute("userSession");
        request.setAttribute("login", login);
        request.setAttribute("departInfo",
            DepartInfoService.selectAllData(login));

        try {
            request.getRequestDispatcher("user/staff/list.jsp")
                   .forward(request, response);
        } catch (ServletException e) {
        }
    }

    private void getDataList(HttpServletRequest request,
        HttpServletResponse response) {
        LoginModel login = (LoginModel) request.getSession()
                                               .getAttribute("userSession");
        Integer page = (request.getParameter("page") == null) ? 1
                                                              : Integer.parseInt(request.getParameter(
                    "page"));
        Integer pageSize = (request.getParameter("pageSize") == null) ? 20
                                                                      : Integer.parseInt(request.getParameter(
                    "pageSize"));
        Staff q = new Staff();
        String staffName = request.getParameter("staffName");

        if ((staffName != null) && (staffName.trim().equals("") == false)) {
            q.setStaffName(staffName);
        }

        String departId = request.getParameter("departId");

        if ((departId != null) && (departId.trim().equals("") == false)) {
            q.setDepartId(Integer.parseInt(departId));
        }

        Map<String, Object> rs = StaffService.selectDataByPage(q, page, pageSize);
        writeResponse(response, rs);

        return;
    }

    public void writeResponse(HttpServletResponse response, Object rs) {
        try {
            response.getWriter().write(JSONObject.fromObject(rs).toString());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
        }
    }
}

